$(function () {
    // getProductData(1, 0);
    loadProduct();
});
function loadProduct() {
    let product_type = sessionStorage.getItem("product_type");
    sessionStorage.removeItem("product_type");//获取之后马上清除，以不影响其他页面的操作
    getProductData(1, product_type);
}
function getProductData(page, product_type) {
    var pageNum = 1;
    var productType = 0;
    if (page == null || page == '')
        page = pageNum;
    if (product_type == null || product_type == '')
        product_type = productType;
    let data = myAjax('/product', 'post', {"product_type": product_type, "page": page}, 'json');
    sessionStorage.setItem("productData", JSON.stringify(data));
    $(".pdt_type").html("");
    $.each(data.productTypes, function (i, producttype) {
        if ((i+1)==product_type)
            $(".pdt_type").append("<div class='currentProductType' onclick='getProductData(1," + producttype.id + ")'>" + producttype.name + "</div>");
        else
        $(".pdt_type").append("<div onclick='getProductData(1," + producttype.id + ")'>" + producttype.name + "</div>");
    });
    let pageCount = Math.ceil(data.pageCount / 10);
    $(".pdt_details").html("");
    $("#page").html("");
    if (page <= pageCount) {
        $.each(data.products, function (i, product) {
            $(".pdt_details").append("<div onclick='redirectProductDetails(" + i + ")'><div><img src=" + product.photo + " height='90%' width='90%'/></div><div class='name'>" + product.name + "</div><div class='type'>" + product.productType.name + "</div><div><div class='price'>价格：</div><div class='num'>" + product.price + "</div></div></div>");
        });
        $("#page").html("<div id='last' onclick='getProductData(" + (parseInt(page) - 1) + "," + product_type + ")'>&lt;</div>" +
            "<div id='right' onclick='getProductData(" + (parseInt(page) + 1) + "," + product_type + ")'>&gt;</div>");
        if (pageCount <= 5) {
            for (let i = pageCount; i >= 1; i--) {
                if (i == page)
                    $("#last").after("<div class='currentPage' onclick='getProductData(" + i + "," + product_type + ")'>" + i + "</div>");
                else
                    $("#last").after("<div onclick='getProductData(" + i + "," + product_type + ")'>" + i + "</div>");
            }
        } else {
            if (pageCount - page >= 5) {
                $("#last").after("<div>...</div><div onclick='getProductData(" + pageCount + "," + product_type + ")'>" + pageCount + "</div>");
                for (let i = page + 4; i >= page; i--) {
                    if (i == page)
                        $("#last").after("<div class='currentPage' onclick='getProductData(" + i + "," + product_type + ")'>" + i + "</div>");
                    else
                        $("#last").after("<div onclick='getProductData(" + i + "," + product_type + ")'>" + i + "</div>");
                }
            } else {
                for (let i = pageCount; i >= pageCount - 4; i--) {
                    if (i == page)
                        $("#last").after("<div class='currentPage' onclick='getProductData(" + i + "," + product_type + ")'>" + i + "</div>");
                    else
                        $("#last").after("<div onclick='getProductData(" + i + "," + product_type + ")'>" + i + "</div>");
                }
            }
        }
    }
};

function redirectProductDetails(i) {
    sessionStorage.setItem("pdti", i);
    window.location.href = "/html/front/productdetails.html";
}